package com.juzi.wopu.web;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.Shop;
import com.juzi.wopu.domain.ShopResponse;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.ShopQuery;
import com.juzi.wopu.service.IImageService;
import com.juzi.wopu.service.IShopService;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.impl.IImageServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class ShopAction extends CRUDAaction<Shop> {
	
	private static final long serialVersionUID = 1L;
	
	private IShopService shopService;
	
	private ISysCodeService sysCodeService;
	
	private IImageService imageService=new IImageServiceImpl();
	
	private Shop shop = new Shop();
	
	private ShopQuery baseQuery = new ShopQuery();
	
	private PageList pageList;
	
	private List<ShopResponse> data=new ArrayList<>();
	
	public List<ShopResponse> getData() {
		return data;
	}

	public void setData(List<ShopResponse> data) {
		this.data = data;
	}

	private File upload;
	private String contentType;
	private String fileName;
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	public String getUploadContentType() {
		return contentType;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUploadFileName() {
		return fileName;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public void setShopService(IShopService shopService) {
		this.shopService = shopService;
	}

	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	/**
	 * 改变状态 使用和未使用
	 * @return
	 */
	public String upDownStatus(){
		baseQuery.setCurrentPage(Integer.MAX_VALUE);
		shopService.updateStatus(id);
		return RELOAD;
	}
	
	@Override
	protected void list() {
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		Map<String, SysCode> map=new HashMap<>();
		Map<Long, SysCode> map1=new HashMap<>();
		sysCodeService.getSysCodeMap(map1, map);//得到sName对应SysCode的map
		Long companyCode=map.get(companyName).getiId();//根据传过来的局点名得到局点的代码，如1902
		baseQuery.setCompanyCode(companyCode);
		Collection<SysCode> list=map.values();//查询出来的所有的SysCode
		
		this.pageList = shopService.findByQuery(baseQuery);//查询
		
		List<SysCode> allSyscode=new ArrayList<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				if(sysCode.getiType()!=null && sysCode.getiType()==4L){
					allSyscode.add(sysCode);
				}
			}
		}
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			@SuppressWarnings("unchecked")
			List<Object> rows=pageList.getRows();
			for(Object pro:rows){
				ShopResponse response=new ShopResponse((Shop)pro);
				String imgPath=imageService.downloadSmallImg(((Shop)pro).getsImgPath());
				response.setSmallImgPath(imgPath);
				SysCode sysCode=map1.get(((Shop)pro).getiCodeId());
				
				response.setShopType(sysCode.getSName());
				data.add(response);
			}
		}
		putContext("allSyscode", allSyscode);
	}

	@Override
	public String input() throws Exception {
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> allCompanyCode=new ArrayList<>();
		if(list!=null && list.size()>0){			
			for(SysCode sysCode:list){
				if(sysCode.getiParentId()!=null && sysCode.getiParentId()==99L){					
					allCompanyCode.add(sysCode);
				}
			}
		}
		putContext("allCompanyCode", allCompanyCode);
		return INPUT;
	}

	public void validateSave() {
		if (StringUtils.isBlank(shop.getsName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	@Override
	@InputConfig(methodName = "input")
	public String save() throws Exception {
		
		SysCode sysCode = shop.getSysCode();
		if(sysCode != null && sysCode.getiId() == -1L){
			shop.setSysCode(null);
		}
		if (id == null) {// 添加
			if(upload!=null){
				String imgPath=imageService.upload(upload, shop.getICompany()+"",645,260);
				shop.setsImgPath(imgPath);
			}else {
				shop.setsImgPath("");
			}
			
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
			Map<String, SysCode> nameCodeMap=new HashMap<>();
			sysCodeService.getSysCodeMap(null, nameCodeMap);//得到sName对应SysCode的map
			
			shop.setSysCode(nameCodeMap.get(companyName));
			
			shop.setDtInsert(new Date());
			
			shop.setiStatus(0L);
			
		}else {
			if(upload!=null){
				String imgPath=imageService.upload(upload, shop.getICompany()+"",645,260);
				shop.setsImgPath(imgPath);
			}
			
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
			Map<String, SysCode> nameCodeMap=new HashMap<>();
			sysCodeService.getSysCodeMap(null, nameCodeMap);//得到sName对应SysCode的map
			
			shop.setSysCode(nameCodeMap.get(companyName));
		}
			
		shopService.save(shop);
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				shopService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			} else {
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:" + e.getMessage() + "\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			shop = shopService.get(id);// 变成栈顶对象
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			shop = new Shop();
		} else {
			shop = shopService.get(id);
			shop.setSysCode(null);
		}
	}
	
	@Override
	public Shop getModel() {
		return shop;
	}

	public PageList getPageList() {
		return pageList;
	}

	public ShopQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(ShopQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}
}
